package LineCode.剑指Offer.链表;

import java.util.ArrayList;

public class 从尾到头打印链表 {

    public static void main(String[] args) {

    }

    /**
     * 暴力笨方法
     * @param listNode
     * @return
     */
    public ArrayList<Integer> printListFromTailToHead2(ListNode listNode) {
        ArrayList<Integer> result = new ArrayList<>();
        ArrayList<Integer> nums = new ArrayList<>();
        while(null != listNode) {
            result.add(listNode.val);
            listNode = listNode.next;
        }

        for (int i = result.size()-1; i >= 0; i--) {
            nums.add(result.get(i));
        }

        return nums;
    }

    /**
     * 递归精简方法
     * @param listNode
     * @return
     */
    ArrayList<Integer> numList = new ArrayList<>();
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if (listNode != null) {
            printListFromTailToHead(listNode.next);
            numList.add(listNode.val);
        }
        return numList;
    }



    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}
